/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package QLThuVien.BLL;

import QLThuVien.DAL.MyConnectUnit;
import QLThuVien.DTO.*;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.HashSet;
/**
 *
 * @author Rado
 */
public class BBCMuonSach {
    MyConnectUnit connect;
    
    public BBCMuonSach(){
        //this.connect = new MyConnectUnit("localhost", "root", "123456", "QLTV");
        this.connect = new MyConnectUnit("", "sa", "123456", "QLTV");
    }
    
    public HashSet<BCMuonSach> getBCMuonSachs(String Condition, String OrderBy) throws Exception{
         ResultSet result = this.connect.Select("BC_MUONSACH", Condition, OrderBy);
         HashSet<BCMuonSach> bcmuonsachs = new HashSet<BCMuonSach>();
         while(result.next()){
             BCMuonSach bcmuonsach = new BCMuonSach();
             bcmuonsach.setMaBCMS(result.getString("MaBCMS"));
             bcmuonsach.setThang(result.getString("Thang"));
             bcmuonsach.setNam(result.getString("Nam"));
             bcmuonsach.setTongLuotMuon(result.getString("TongLuotMuon"));             
             bcmuonsachs.add(bcmuonsach);
         }
         return bcmuonsachs;
     }
    
    public HashSet<BCMuonSach> getBCMuonSachs(String Condition) throws Exception{
        return getBCMuonSachs(Condition, null);
    }
    
    public HashSet<BCMuonSach> getBCMuonSachs() throws Exception{
        return getBCMuonSachs(null);
    }
    
    public BCMuonSach getByID(String id) throws Exception{
        HashSet<BCMuonSach> bcmuonsachs = this.getBCMuonSachs("MaBCMS = " + id);
        if(bcmuonsachs.size() > 0)
            return bcmuonsachs.toArray(new BCMuonSach[bcmuonsachs.size()])[0];
        return null;
    }
    
    public void InsertBCMuonSach(BCMuonSach bcmuonsach) throws Exception{
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("MaBCMS", bcmuonsach.getMaBCMS());       
        map.put("Thang", bcmuonsach.getThang());
        map.put("Nam", bcmuonsach.getNam());       
        map.put("TongLuotMuon", bcmuonsach.getTongLuotMuon()); 
        this.connect.Insert("BC_MUONSACH", map);
    }
    
    public void InsertBCMuonSach(HashSet<BCMuonSach> bcmuonsachs) throws Exception{
        for(BCMuonSach bcmuonsach:bcmuonsachs)
            this.InsertBCMuonSach(bcmuonsach);
    }
    
    public void UpdateBCMuonSach(BCMuonSach bcmuonsach) throws Exception{
        HashMap<String, Object> map = new HashMap<String, Object>(); 
        map.put("Thang", bcmuonsach.getThang());
        map.put("Nam", bcmuonsach.getNam());       
        map.put("TongLuotMuon", bcmuonsach.getTongLuotMuon());      
        this.connect.Update("BC_MUONSACH", map, "MaBCMS = " + bcmuonsach.getMaBCMS());
    }
    
    public void UpdateBCMuonSach(HashSet<BCMuonSach> bcmuonsachs) throws Exception{
        for(BCMuonSach bcmuonsach:bcmuonsachs)
            this.UpdateBCMuonSach(bcmuonsach);
    }
    
    public void DeleteBCMuonSach(BCMuonSach bcmuonsach) throws Exception{
        this.connect.Delete("BC_MUONSACH", "MaBCMS = " + bcmuonsach.getMaBCMS());
    }
    
    public void DeleteBCMuonSach(HashSet<BCMuonSach> bcmuonsachs) throws Exception{
        for(BCMuonSach bcmuonsach:bcmuonsachs)
            this.DeleteBCMuonSach(bcmuonsach);
    }
}
